/**
 * @(#) FinalSemesterResultStatisticsPDFView.java
 * Copyright (c) 2011 EdRP, Dayalbagh Educational Institute.
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this  list of conditions and the following disclaimer.
 *
 * Redistribution in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the
 * distribution.
 *
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL ETRG OR ITS CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Contributors: Members of EdRP, Dayalbagh Educational Institute
 */

package in.ac.dei.edrp.cms.controller.reportgeneration;

import in.ac.dei.edrp.cms.dao.reportgeneration.FinalSemesterResultStatisticsDAO;
import in.ac.dei.edrp.cms.domain.reportgeneration.FinalSemesterResultStatistics;
import in.ac.dei.edrp.cms.domain.reportgeneration.SubjectWiseMeritList;

import java.awt.Color;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.view.document.AbstractPdfView;

import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfWriter;

public class FinalSemesterResultStatisticsPDFView extends AbstractPdfView {
	
	private FinalSemesterResultStatisticsDAO finalSemesterResultStatisticsDao;	

	public void setFinalSemesterResultStatisticsDao(
			FinalSemesterResultStatisticsDAO finalSemesterResultStatisticsDao) {
		this.finalSemesterResultStatisticsDao = finalSemesterResultStatisticsDao;
	}



	@SuppressWarnings("unchecked")
	protected void buildPdfDocument(Map model,Document document,PdfWriter writer,
			HttpServletRequest request,HttpServletResponse response) throws Exception
	{
		System.out.println("inside pdf view controller");
				
		List <FinalSemesterResultStatistics> programDetails;
		List<FinalSemesterResultStatistics> completeDetails=new ArrayList<FinalSemesterResultStatistics>();
		String sessionStartDate=request.getParameter("sessionStartDate");
		String sessionEndDate=request.getParameter("sessionEndDate");
		
//		document = new Document(PageSize.A4.rotate());
		writer = PdfWriter.getInstance(document,new FileOutputStream("E:\\Final Semester Result Statistics  "+sessionStartDate+"  -  "+sessionEndDate+".pdf"));
		document.setPageSize(PageSize.A4.rotate());
		document.open();
		
		String headerText1 = "Final Semester Result Statistics :"+ sessionStartDate+"  -  "+sessionEndDate;
		Paragraph header1 = new Paragraph(headerText1,FontFactory.getFont(FontFactory.HELVETICA,8, Font.NORMAL, new Color(0, 0, 255)));		
		
		String printingDate = new Date().toString();
		Paragraph header2 = new Paragraph(printingDate,FontFactory.getFont(FontFactory.HELVETICA,8, Font.NORMAL, new Color(0, 0, 255)));
		
		header1.setAlignment(Element.ALIGN_CENTER);
		document.add(header1);
		document.add(header2);
		
		programDetails = (List <FinalSemesterResultStatistics>) model.get("getProgramDetail");
		
		FinalSemesterResultStatistics finalSemesterResultStatistics=new FinalSemesterResultStatistics();
		finalSemesterResultStatistics.setProgramCourseKey(programDetails.get(0).getProgramCourseKey());
		finalSemesterResultStatistics.setProgramId(programDetails.get(0).getProgramId());
		finalSemesterResultStatistics.setProgramCode(programDetails.get(0).getProgramCode());
		finalSemesterResultStatistics.setBranchId(programDetails.get(0).getBranchId());
		finalSemesterResultStatistics.setSpecializationId(programDetails.get(0).getSpecializationId());
		finalSemesterResultStatistics.setSemesterCode(programDetails.get(0).getSemesterCode());
		finalSemesterResultStatistics.setEntityId(request.getParameter("selectedEntityId"));
		completeDetails=finalSemesterResultStatisticsDao.getCompleteDetails(finalSemesterResultStatistics);
		
		FinalSemesterResultStatistics entityObject=finalSemesterResultStatisticsDao.getEntityInfo(finalSemesterResultStatistics);
		
		Font cellFont = new Font(Font.HELVETICA, 8);
		Table studentTable = new Table(14,3);
				
				studentTable.setBorderWidth(1);
				
				Cell c1 = new Cell(new Phrase("F-CL-BR-S ",cellFont));		
				c1.setHeader(true);
				c1.setColspan(1);
				c1.setBorderWidth(0);
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);
				
				c1 = new Cell(new Phrase(" SEX ",cellFont));
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				c1.setBorderWidth(0);
				studentTable.addCell(c1);
				
				c1 = new Cell(new Phrase(" ENROLLED \n ",cellFont));
				c1.setColspan((1));
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				c1.setBorderWidth(0);
				studentTable.addCell(c1);
				
				c1 = new Cell(new Phrase(" APPEARED ",cellFont));
				c1.setColspan((1));
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				c1.setBorderWidth(0);
				studentTable.addCell(c1);		
				
				c1 = new Cell(new Phrase(" UFM ",cellFont));
				c1.setColspan(1);
				c1.setBorderWidth(0);
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);
				
				/*c1 = new Cell(new Phrase(" INCOMPL. ",cellFont));
				c1.setColspan(1);
				c1.setBorderWidth(0);
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);*/
				
				c1 = new Cell(new Phrase(" PASSED ",cellFont));
				c1.setColspan(1);
				c1.setBorderWidth(0);
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);
				
				c1 = new Cell(new Phrase(" I-DIST ",cellFont));
				c1.setColspan(1);
				c1.setBorderWidth(0);
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);
				
				c1 = new Cell(new Phrase(" I-Div ",cellFont));
				c1.setColspan(1);
				c1.setBorderWidth(0);
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);
				
				c1 = new Cell(new Phrase(" II-Div ",cellFont));
				c1.setColspan(1);
				c1.setBorderWidth(0);
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);
				
				c1 = new Cell(new Phrase(" PASS ",cellFont));
				c1.setColspan(1);
				c1.setBorderWidth(0);
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);
				
				c1 = new Cell(new Phrase(" ELIGIBLE FOR REMEDIALS ",cellFont));
				c1.setColspan(1);
				c1.setBorderWidth(0);
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);
				
				c1 = new Cell(new Phrase(" FAILED 1ST ATTEMPT ",cellFont));
				c1.setColspan(1);
				c1.setBorderWidth(0);
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);
				
				c1 = new Cell(new Phrase(" FAILED 2ND ATTEMPT ",cellFont));
				c1.setColspan(1);
				c1.setBorderWidth(0);
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);
				
				c1 = new Cell(new Phrase(" PASSED % ",cellFont));
				c1.setColspan(1);
				c1.setBorderWidth(0);
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);
	    
				for(int i=0;i<completeDetails.size();i++){
					c1 = new Cell(new Phrase(entityObject.getEntityCode()+"-"+programDetails.get(0).getProgramCode()+"-"+programDetails.get(0).getBranchId()+"-"+programDetails.get(0).getSemesterCode(),cellFont));
					c1.setColspan(1);
					c1.setBorderWidth(0);
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					studentTable.addCell(c1);
					
					c1 = new Cell(new Phrase(completeDetails.get(i).getSex(),cellFont));
					c1.setColspan(1);
					c1.setBorderWidth(0);
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					studentTable.addCell(c1);
					
					c1 = new Cell(new Phrase(completeDetails.get(i).getEnrolled(),cellFont));
					c1.setColspan(1);
					c1.setBorderWidth(0);
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					studentTable.addCell(c1);
					
					c1 = new Cell(new Phrase(completeDetails.get(i).getAppeared(),cellFont));
					c1.setColspan(1);
					c1.setBorderWidth(0);
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					studentTable.addCell(c1);
					
					c1 = new Cell(new Phrase(completeDetails.get(i).getUfm(),cellFont));
					c1.setColspan(1);
					c1.setBorderWidth(0);
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					studentTable.addCell(c1);
					
					c1 = new Cell(new Phrase(completeDetails.get(i).getPassed(),cellFont));
					c1.setColspan(1);
					c1.setBorderWidth(0);
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					studentTable.addCell(c1);
					
					c1 = new Cell(new Phrase(completeDetails.get(i).getIDistinction(),cellFont));
					c1.setColspan(1);
					c1.setBorderWidth(0);
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					studentTable.addCell(c1);
					
					c1 = new Cell(new Phrase(completeDetails.get(i).getIDiv(),cellFont));
					c1.setColspan(1);
					c1.setBorderWidth(0);
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					studentTable.addCell(c1);
					
					c1 = new Cell(new Phrase(completeDetails.get(i).getIIDiv(),cellFont));
					c1.setColspan(1);
					c1.setBorderWidth(0);
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					studentTable.addCell(c1);
					
					c1 = new Cell(new Phrase(completeDetails.get(i).getPass(),cellFont));
					c1.setColspan(1);
					c1.setBorderWidth(0);
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					studentTable.addCell(c1);
					
					c1 = new Cell(new Phrase(completeDetails.get(i).getRemedial(),cellFont));
					c1.setColspan(1);
					c1.setBorderWidth(0);
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					studentTable.addCell(c1);
					
					c1 = new Cell(new Phrase(completeDetails.get(i).getFailedFirstAttempt(),cellFont));
					c1.setColspan(1);
					c1.setBorderWidth(0);
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					studentTable.addCell(c1);
					
					c1 = new Cell(new Phrase(completeDetails.get(i).getFailedSecondAttempt(),cellFont));
					c1.setColspan(1);
					c1.setBorderWidth(0);
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					studentTable.addCell(c1);
					
					c1 = new Cell(new Phrase(completeDetails.get(i).getPercent(),cellFont));
					c1.setColspan(1);
					c1.setBorderWidth(0);
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					studentTable.addCell(c1);
				}
			
			studentTable.setBorderWidth(0);		
			studentTable.endHeaders();		
			document.add(studentTable);
			document.close();		
	}
	
}

